package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.RoleMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.User;
import com.heima.pojo.domain.PageQuery;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.domain.Role;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.service.UserService;
import com.heima.utils.SecretKeyGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SecretKeyGenerator secretKeyGenerator;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null){
            //参数异常
            throw new RuntimeException("参数为null,异常");
        }
//        LoginUserVo loginUserVo = userMapper.login(dto.getLoginName(), dto.getPassword());
//        return loginUserVo;

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName,dto.getLoginName());
        User user = userMapper.selectOne(wrapper);

        if(user == null){ //代表用户不存在得

        }
        if(!user.getPassword().equals(dto.getPassword())){ //代表密码错误

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    /**
     * 人员搜索
     * @param pageQuery
     * @param userName
     * @return
     */

    @Override
    public PageResult search(PageQuery pageQuery, String userName) {
        Integer pageIndex = pageQuery.getPageIndex();
        Integer pageSize = pageQuery.getPageSize();
        Long totalCount = userMapper.getTotal(userName);        //总记录数
        Long totalPage =  (totalCount+pageSize-1) / pageSize;   //总页数
        //起始索引
        Integer startIndex = (pageIndex-1)*pageSize;
        List<User> list = userMapper.getSearch(startIndex,pageSize,userName);
        return new PageResult<>(pageIndex,pageSize,totalPage,totalCount,list);
    }

    /**
     * 新增人员
     * @param userDto
     * @return
     */
    @Override
    public Boolean insert(UserDto userDto) {
        Role role = roleMapper.selectOne(Wrappers.<Role>lambdaQuery().eq(Role::getRoleId, userDto.getRoleId()));
        User user = new User();
        user.setRoleCode(role.getRoleCode());
        user.setSecret(secretKeyGenerator.generateSecretKey());
        BeanUtils.copyProperties(userDto,user);
        return userMapper.insert(user)>0;
    }

    @Override
    public Boolean updateUser(UserDto userDto, Integer userId) {
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        return userMapper.update(user,Wrappers.<User>lambdaUpdate().eq(User::getId,userId)) >0;
    }
}
